﻿using System;
using System.Net.Mail;
using System.IO;

namespace StudentInfo.EmailServices
{
    public class EmailService
    {
        public static bool SendVerifyNewUserMessage(Account account, string userPassword,
           string urlBase, string verifyUrlBase, string appPath)
        {
            try
            {
                string verifyNewUser = verifyUrlBase + "?Id=" + account.Id.ToString();
                string fullPath = urlBase + verifyNewUser;

                var sr = new StreamReader(appPath + "EmailServices/EmailTemplates/VerifyNewUser.txt");
                var mailMessage = new System.Net.Mail.MailMessage();
                mailMessage.IsBodyHtml = true;
                mailMessage.To.Add(account.Email);
                mailMessage.Subject = "Registration on TestStudInfo";
                mailMessage.Body = sr.ReadToEnd();
                sr.Close();

                mailMessage.Body = mailMessage.Body.Replace("@AccountName", account.AccountName);
                mailMessage.Body = mailMessage.Body.Replace("@VerificationUrl", fullPath);
                mailMessage.Body = mailMessage.Body.Replace("@Password", userPassword);

                var smtpClient = new SmtpClient();
                smtpClient.Send(mailMessage);

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}